require 'rake'
require 'rake/clean'
require 'rake/testtask'
require 'rake/rdoctask'
require 'rake/gempackagetask'
require 'rake/contrib/sshpublisher'
require 'rake/contrib/rubyforgepublisher'
require File.join(File.dirname(__FILE__), 'lib', 'active_test', 'version')

#### STANDARD TASKS

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the active_test plugin.'
Rake::TestTask.new(:test) do |t|
  t.libs << 'lib'
  t.pattern = 'test/**/*_test.rb'
  t.verbose = true
end

desc 'Generate documentation for the active_test plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
  rdoc.rdoc_dir = 'rdoc'
  rdoc.title    = 'ActiveTest'
  rdoc.options << '--line-numbers' << '--inline-source'
  rdoc.rdoc_files.include('README')
  rdoc.rdoc_files.include('lib/**/*.rb')
end

desc "Generate a coverage report for all the tests."
task :rcov do

  RBINDIR = Config::CONFIG['bindir']
  RBIN = "#{RBINDIR}/rcov"
  if File.exists?(RBIN)

    options = ARGV.dup; options.shift

    # don't report coverage on these files
    RCOV_EXCLUDE = %w(init.rb install.rb).join(',')

    # RCOV command, run as though from the commandline.  Amend as required or perhaps move to config/environment.rb?
    RCOV = "#{RBINDIR}/ruby #{RBIN} -I lib --sort coverage --sort-reverse --exclude #{RCOV_EXCLUDE} --text-report --no-html"
    RCOV << " #{options.join(' ')}" unless options.empty?

    sh "#{RCOV} " << Dir.glob("test/**/*_test.rb").join(' ')
  else
    puts "Error: please install rcov"
  end
end

desc "Generate a coverage report for all the tests and output html to coverage directory."
task :rcov_with_html do

  RBINDIR = Config::CONFIG['bindir']
  RBIN = "#{RBINDIR}/rcov"

  if File.exists?(RBIN)

    RCOV_OUT = "coverage"
    CLOBBER.include(RCOV_OUT)

    options = ARGV.dup; options.shift

    # don't report coverage on these files
    RCOV_EXCLUDE = %w(init.rb install.rb).join(',')

    # RCOV command, run as though from the commandline.  Amend as required or perhaps move to config/environment.rb?
    RCOV = "#{RBINDIR}/ruby #{RBIN} --sort coverage --sort-reverse --exclude #{RCOV_EXCLUDE} --output #{RCOV_OUT}"
    RCOV << " #{options.join(' ')}" unless options.empty?

    sh "#{RCOV} " << Dir.glob("test/**/*_test.rb").join(' ')
  else
    puts "Error: please install rcov"
  end
end

#### GEM PACKAGING

PKG_VERSION               = ActiveTest::VERSION::STRING

PKG_NAME                  = "active_test"
PKG_FILE_NAME             = "#{PKG_NAME}-#{PKG_VERSION}"

RUBY_FORGE_PROJECT        = "activetest"
RUBY_FORGE_USER           = "mabs29"

spec = Gem::Specification.new do |s|
  s.name            = PKG_NAME
  s.version         = PKG_VERSION
  s.platform        = Gem::Platform::RUBY
  s.summary         = "Rails-Style Testing Framework"
  s.description     = <<-DESC
ActiveTest takes the repetition out of Test::Unit cases, making tests focus on behaviour and
requirements instead of methods and assertions. It uses a simple core which: makes Test::Unit
extensible; offers ways to safely meta-program; enables inheritance; allows meta-programming
techniques; reads more easily; and encourages DRY yet informative tests. Lastly, it provides
template classes which encourage Rails-standard conventions.
  DESC
 
  s.require_path    = "lib"
  s.autorequire     = PKG_NAME
  s.has_rdoc        = true
  s.test_files      = Dir["test/**/*_test.rb"]
  s.add_dependency    "rails", ">= 1.1.4"
 
  s.author          = "Mathew Abonyi"
  s.email           = "mabs@mathewabonyi.com"
  s.homepage        = "http://www.mathewabonyi.com/"

  s.files           = %w{Rakefile install.rb README CHANGELOG TODO}
  s.files           = s.files + Dir.glob("lib/**/*").delete_if { |i| i.include?("\.svn") }
  s.files           = s.files + Dir.glob("test/**/*").delete_if { |i| i.include?("\.svn") }
end

Rake::GemPackageTask.new(spec) do |p|
  p.gem_spec = spec
  p.need_tar = true
  p.need_zip = true
end

desc "Publish the API documentation"
task :pdoc => [:rdoc] do
  Rake::RubyForgePublisher.new(RUBY_FORGE_PROJECT, RUBY_FORGE_USER).upload
end
 
desc "Publish the API docs and gem"
task :publish => [:pdoc, :release]
 
desc "Publish the release files to RubyForge."
task :release => [:gem, :package] do
  `rubyforge login`

  for ext in %w( gem tgz zip )
    release_command = "rubyforge add_release #{PKG_NAME} #{PKG_NAME} 'REL #{PKG_VERSION}' pkg/#{PKG_NAME}-#{PKG_VERSION}.#{ext}"
    puts release_command
    system(release_command)
  end
end

